﻿using Kimd.Common;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WorkStation.Common
{
    public class ThreeColorLight : SingletonProvider<ThreeColorLight>
    {
        private Cylinder _greenLight;
        private Cylinder _redLight;
        private Cylinder _yellowLight;
        private Cylinder _warnBeep;
        public ThreeColorLight()
        {

        }
        public ThreeColorLight(Cylinder green, Cylinder red, Cylinder yellow, Cylinder beep)
            : this()
        {
            this._greenLight = green;
            this._redLight = red;
            this._yellowLight = yellow;
            this._warnBeep = beep;
        }
        /// <summary>
        /// 正常运行时，绿灯亮，红黄灯灭，蜂鸣器关
        /// </summary>
        public void Run()
        {
            if (GlobalVariable.IsEcoMode)
                return;
            if (_greenLight != null)
                _greenLight.On();
            if (_redLight != null)
                _redLight.Off();
            if (_yellowLight != null)
                _yellowLight.Off();
            BeepOff();
        }
        /// <summary>
        /// 暂停时，绿灯灭，红灯灭，黄灯亮，蜂鸣器关
        /// </summary>
        public void Pause()
        {
            if (GlobalVariable.IsEcoMode)
                return;
            if (_greenLight != null)
                _greenLight.Off();
            if (_redLight != null)
                _redLight.Off();
            if (_yellowLight != null)
                _yellowLight.On();
            BeepOff();
        }
        public void Stop()
        {
            if (GlobalVariable.IsEcoMode)
                return;
            if (_greenLight != null)
                _greenLight.Off();
            if (_redLight != null)
                _redLight.Off();
            if (_yellowLight != null)
                _yellowLight.On();
            BeepOff();
        }
        /// <summary>
        /// 报警时，红灯亮，蜂鸣器开，黄绿灯灭
        /// </summary>
        public void Warn()
        {
            if (GlobalVariable.IsEcoMode)
                return;
            if (_greenLight != null)
                _greenLight.Off();
            if (_redLight != null)
                _redLight.On();
            if (_yellowLight != null)
                _yellowLight.Off();
            BeepOn();
        }
        public void Close()
        {
            if (GlobalVariable.IsEcoMode)
                return;
            if (_greenLight != null)
                _greenLight.Off();
            if (_redLight != null)
                _redLight.Off();
            if (_yellowLight != null)
                _yellowLight.Off();            
            BeepOff();   
        }
        public void Home()
        {
            if (GlobalVariable.IsEcoMode)
                return;
            if (_greenLight != null)
                _greenLight.Off();
            if (_redLight != null)
                _redLight.Off();
            if (_yellowLight != null)
                _yellowLight.Off();
            BeepOff();
        }
        public void BeepOff()
        {
            if (beepTask != null)
                beepTask.Abort();
            Thread.Sleep(100);
            if (_warnBeep != null)
                _warnBeep.Off();
        }
        Thread beepTask;
        public void BeepOn()
        {
            if (_warnBeep != null)
            {
                if (this.beepTask == null || !this.beepTask.IsAlive)
                {
                    this.beepTask = new Thread(new ThreadStart(() =>
                    {
                        if (_warnBeep != null)
                        {
                            while (true && !GlobalVariable.IsEcoMode)
                            {
                                _warnBeep.On();
                                Thread.Sleep(500);
                                _warnBeep.Off();
                                Thread.Sleep(500);
                            }
                            if (!GlobalVariable.IsEcoMode)
                                _warnBeep.Off();
                        }
                    }));
                    this.beepTask.IsBackground = true;
                    this.beepTask.Start();
                }
            }
        }
    }
}
